package com.mini.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Zheng Jie
 * @date 2018-11-24
 */
@Slf4j
@Configuration
// 自动配置
public class RedisConfig {

    /**
     * 用户信息缓存前缀
     */
    private static final String USER_KEY_PREFIX="mini:business";

    /**
     * redis中间前缀
     */
    private static final String REDIS_SPLIT_PRE = ":";

    public static class CacheKeys {
        private CacheKeys() {
        }
        public static final String item_info_all_list = USER_KEY_PREFIX + REDIS_SPLIT_PRE +"itemInfo:all:list";

    }
    private enum CacheEnum {

        /**
         * 两分钟失效
         */
        item_info_all_list(CacheKeys.item_info_all_list, 60*2),
        ;

        public final String cacheName;
        public final int expireInSeconds;

        CacheEnum(String cacheName, int expireInSeconds) {
            this.cacheName = cacheName;
            this.expireInSeconds = expireInSeconds;
        }
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setUsePrefix(true);
        cacheManager.setDefaultExpiration(60);
        Map<String, Long> expires = Arrays.stream(RedisConfig.CacheEnum.values())
                .collect(Collectors.toMap(e -> e.cacheName, e -> Long.valueOf(e.expireInSeconds)));

        cacheManager.setExpires(expires);
        return cacheManager;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.setSerializationInclusion(Include.NON_NULL);
        om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }

}
